package com.bw.sparkstreaming.job7
import org.apache.log4j.{Level, Logger}
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * 窗口统计
 * 每隔2秒统计最近4秒的单词出现的情况
 */
object WindowAndWordCount {
  def main(args: Array[String]): Unit = {
    Logger.getLogger("org").setLevel(Level.ERROR)

    val sparkConf = new SparkConf().setAppName("windowAndWordCount").setMaster("local[2]")
    val sc = new SparkContext(sparkConf)
    val ssc = new StreamingContext(sc, Seconds(2))

    val lines:ReceiverInputDStream[String] = ssc.socketTextStream("192.168.134.130", 9999)
    val words:DStream[String] = lines.flatMap(_.split(","))
    val wordsDStream = words.map(x => (x, 1))

    /**
     * 每隔2秒统计最近4秒的情况
     * reduceFunc: (V, V) => V,
     * windowDuration: Duration, 窗口的大小 4
     * slideDuration: Duration  滑动的大小 2
     *
     * Storm
     */
    val result = wordsDStream.reduceByKeyAndWindow((x:Int,y:Int) => x+y,Seconds(4),Seconds(2))

    result.print()

    ssc.start()
    ssc.awaitTermination()
    ssc.stop()
  }
}
